packageurl <- "https://cran.r-project.org/src/contrib/Archive/rdrobust/rdrobust_0.80.tar.gz"
install.packages(packageurl, repos=NULL, type="source")

library(rdrobust)
library(foreign)
library(plyr)
library(ggplot2)
library(readstata13)

# load data of alumni only
setwd("/Users/moch/Dropbox/TFA_analysis/APSR Replication")
d_overall <- read.dta13("TFA_FinalDataset.dta")
d <- filter(d_overall, appyear<2014)


# Response Rate Graphs (Figure A.2)  
t3 <- d$complete
t4 <- d$started
admit <- d$zscore

(rdplot(y = t3, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "Response Rate (RR1)", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,0.4)))
(rdplot(y = t4, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "Response Rate (RR2)", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,0.4)))


# First Stage Graphs (Figure A.3) - IMSE-optimal bins  
t <- d$admit
t2 <- d$matriculated4

(rdplot(y = t, x = admit, numbinl = 136, numbinr = 161, p=6, title = "", y.label = "Admission Rate", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-2,2), y.lim = c(0,1)))
(rdplot(y = t2, x = admit, numbinl = 148 , numbinr = 120, p=6, title = "", y.label = "Participation Rate", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-2,2), y.lim = c(0,1)))


# Demographic Graphs (Figures D.4-D.5)
age <-d$age3 
female <-d$female3 
white <-d$white2
gpa <-d$gpa2 
selective <-d$schoolselective
parentalcollege <-d$parentalcollege2 
pell <-d$pell
class1 <-d$class1
class2 <-d$class2
class3 <-d$class3
class4 <-d$class4
class5 <-d$class5
religiosity <-d$religiosity 

(rdplot(y = age, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "Age", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(20,40)))
(rdplot(y = female, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "Female", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,1)))
(rdplot(y = white, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "White", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,1)))
(rdplot(y = gpa, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "College GPA", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(3,4)))
(rdplot(y = selective, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "Undergraduate School Selectivity", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,1)))
(rdplot(y = parentalcollege, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "Parental Education", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0.5,1)))
(rdplot(y = pell, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "Received Pell Grant", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,1)))
(rdplot(y = class1, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "Upper Class", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,1)))
(rdplot(y = class2, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "Upper Middle Class", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,1)))
(rdplot(y = class3, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "Lower Middle Class", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,1)))
(rdplot(y = class4, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "Upper Lower Class", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,1)))
(rdplot(y = class5, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "Lower Class", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,1)))
(rdplot(y = religiosity, x = admit, numbinl = 250, numbinr = 250, p=6, title = "", y.label = "Identify with Religion", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,1)))


# Results Graphs (Figures D.6-D.7)
systemsupport <- d$systemsupport
edpol1a_su2_v2 <- d$edpol1a_su2_v2
edpol1a_su6_v2 <- d$edpol1a_su6_v2
edaw1v2 <- d$edaw1v2
edaw1v2_2 <- -(d$edaw1v2-1)
wvs_libindex <- d$wvs_libindex
resent_index <- d$resent_index
discrim_index3 <- d$discrim_index3
iatscore_2 <- d$iatscore_2
tol18_8v2 <- d$tol18_8v2
tol18_14v2 <- d$tol18_14v2

(rdplot(y = systemsupport, x = admit,  numbinl = 100, numbinr = 100, p=4, title = "", y.label = "System Support Index", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0.4,0.6)))
(rdplot(y = edpol1a_su2_v2, x = admit,  numbinl = 100, numbinr = 100, p=4, title = "", y.label = "Agree: Poor Families do Not Value Education", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,0.5)))
(rdplot(y = edpol1a_su6_v2, x = admit, numbinl = 100, numbinr = 100, p=4, title = "", y.label = "Agree: Systemic Injustice Perpetuates Inequity", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0.7,0.95)))
(rdplot(y = edaw1v2_2, x = admit, numbinl = 100, numbinr = 100, p=4, title = "", y.label = "Agree: Educational Opportunities Equal by Income", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,0.2)))
(rdplot(y = wvs_libindex , x = admit, numbinl = 100, numbinr = 100, p=4, title = "", y.label = "Class-Based Injustice Index", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0.5,0.8)))
(rdplot(y = resent_index, x = admit, numbinl = 100, numbinr = 100, p=4, title = "", y.label = "Racial Resentment Index", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,0.4)))
(rdplot(y = discrim_index3, x = admit, numbinl = 100, numbinr = 100, p=4, title = "", y.label = "Satisfaction with Treatment of Minorities Index", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0.2,0.5)))
(rdplot(y = iatscore_2, x = admit, numbinl = 100, numbinr = 100, p=4, title = "", y.label = "Skin Color Implicit Association Test", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,0.5)))
(rdplot(y = tol18_8v2, x = admit, numbinl = 100, numbinr = 100, p=4, title = "", y.label = "Feel Close to Blacks", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,0.5)))
(rdplot(y = tol18_14v2, x = admit, numbinl = 100, numbinr = 100, p=4, title = "", y.label = "Feel Close to Hispanics", x.label = "TFA Admission Score", col.line = "Black", col.dots = "Grey", x.lim = c(-1,1), y.lim = c(0,0.5)))


